package com.tjkxzx.cfnie.core.service.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.tjkxzx.cfnie.core.dao.entity.CfnProperty;
import com.tjkxzx.cfnie.core.dao.mapper.CfnPropertyMapper;
import com.tjkxzx.cfnie.core.service.CfnPropertyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Luc
 * @since 2024/01/05
 */
@Service
@RequiredArgsConstructor
public class CfnPropertyServiceImpl extends ServiceImpl<CfnPropertyMapper, CfnProperty> implements CfnPropertyService {

    private final CfnPropertyMapper cfnPropertyMapper;

    private List<CfnProperty> cfnProperties = new ArrayList<>();

    private Map<String, Integer> headers = new HashMap<>();


    @Override
    public void clearTable() {
        cfnPropertyMapper.truncate();
    }

    @Override
    public List<CfnProperty> readData(String filePath) {
        ExcelUtil.readBySax(filePath, 0, createRowHandler());
        if(cfnProperties.size() > 0) {
            this.saveBatch(cfnProperties);
        }
        return cfnProperties;
    }

    private CfnProperty buildObject(List<Object> target) throws Exception {
        CfnProperty cfnProperty = new CfnProperty();
        Class<CfnProperty> clazz = CfnProperty.class;
        Field[] fields = clazz.getDeclaredFields();
        for(int i=2; i<fields.length; i++) {
            Field field = fields[i];
            field.setAccessible(true);
            ApiModelProperty apiModelProperty = clazz.getDeclaredField(fields[i].getName()).getAnnotation(ApiModelProperty.class);
            if(!Objects.isNull(apiModelProperty)) {
                String alias = apiModelProperty.value();
                if(headers.keySet().contains(alias)) {
                    int idx = headers.get(alias);
                    if(!Objects.isNull(target.get(idx))) {
                        field.set(cfnProperty, target.get(idx).toString().trim());
                    }
                }
            }
        }
        return cfnProperty;
    }

    private RowHandler createRowHandler() {
        return new RowHandler() {

            @Override
            public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {

            }

            @Override
            public void doAfterAllAnalysed() {

            }

            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {
                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
                if(rowIndex == 0) {
                    for(int i=0; i<rowlist.size(); i++) {
                        headers.put(rowlist.get(i).toString(), i);
                    }
                } else {
                    try {
                        CfnProperty cfnProperty = buildObject(rowlist);
                        if(!Objects.isNull(cfnProperty)) {
                            cfnProperties.add(cfnProperty);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }
}
